package hlhj.fhp.supreme.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.hyphenate.chat.EMClient
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.*
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import kotlinx.android.synthetic.main.self_fgm.view.*
import com.hyphenate.chat.EMGroupManager.EMGroupStyle
import com.hyphenate.chat.EMGroupOptions
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.javabean.*
import kotlinx.android.synthetic.main.self_fgm.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.net.URL


/**
 * Created by Administrator on 2018\3\6 0006.
 */
class SelfFgm :Fragment() {
    private lateinit var rootView:View
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater?.inflate(R.layout.self_fgm,null)
        EventBus.getDefault().register(this)
        getUser()
        initListener()
        return  rootView
    }
    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    private fun getUser() {
        OkGo.post<UserInfoBean>(Urls.BaseUrl+ Urls.UserInfo)
                .execute(object : JsonCallBack<UserInfoBean>(activity){
                    override fun onSuccess(response: Response<UserInfoBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            SPHelper.saveUser(context,bean.data)
                            rootView.userNick.text=bean?.data.name
                            Glide.with(context).load(bean?.data.avatar).asBitmap().error(R.mipmap.ic_account).placeholder(R.mipmap.ic_account).into(rootView.userIcon)
                            rootView.userIdentity.text=when(bean?.data.user_type){
                                "2"->{
                                    rootView.btCover.visibility=View.GONE
                                    rootView.btLive.visibility=View.GONE
                                    "学员"

                                }
                                "3"->{
                                    rootView.btCover.visibility=View.VISIBLE
                                    rootView.btLive.visibility=View.VISIBLE
                                    "老师"
                                }
                                else->""
                            }
                            when(bean?.data.end_time){
                                "0"->{
                                    rootView.userLevel.visibility=View.GONE
                                }
                                else->{
                                    if (bean?.data.end_time!=null){
                                        if (bean?.data.end_time.toLong()<System.currentTimeMillis()/1000){
                                            rootView.userLevel.visibility=View.GONE
                                        }else{
                                            rootView.userLevel.visibility=View.VISIBLE
                                        }
                                    }else{

                                    }

                                }
                            }

                        }
                    }
                })

    }

    private fun initListener() {
        /**
         *   创建群组
         */
        rootView.btLive.setOnClickListener {
            context.startActivity(Intent(context,MyJiePanAty::class.java))
//            context.startActivity(Intent(context,StartJiePanAty::class.java))
            /*OkGo.post<RoomBea>(Urls.BaseUrl+Urls.GET_ROOM)
                    .execute(object :JsonCallBack<RoomBea>(activity){
                        override fun onSuccess(response: Response<RoomBea>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                if (bean?.data!=null){
                                    val intent = Intent(activity, JiePanDetailAty::class.java)
                                    intent.putExtra("roomId",bean?.data.chat_id)
                                    startActivity(intent)
                                }
                            }else{
                                val intent = Intent(context, StartJiePanAty::class.java)
                                startActivity(intent)
                            }
                        }
                    })*/
        }
        rootView.btGain.setOnClickListener {
            context.startActivity(Intent(context,SelfGainAty::class.java))

        }
        rootView.btMyExtension.setOnClickListener {
            context.startActivity(Intent(context,JoinExtensionAty::class.java))
        }
        rootView.userIcon.setOnClickListener {
            context.startActivity(Intent(context,SelfInfoAty::class.java))
        }
        rootView.btJoin.setOnClickListener {
            context.startActivity(Intent(context,MyExtension::class.java))
        }

        rootView.btSign.setOnClickListener {
            context.startActivity(Intent(context,SignAty::class.java))
        }
        rootView.btMyCollection.setOnClickListener{
            context.startActivity(Intent(context,MyCollectionsAty::class.java))
        }
        rootView.btPointMall.setOnClickListener {
            context.startActivity(Intent(context,PointMallAty::class.java))

        }
        rootView.btMyVip.setOnClickListener {
            context.startActivity(Intent(context,MyVipAty::class.java))
        }
        rootView.btLearnCard.setOnClickListener {
            context.startActivity(Intent(context,LearnCardAty::class.java))
        }
        rootView.btHelpCenter.setOnClickListener{
            context.startActivity(Intent(context,HelpCenterAty::class.java))

        }
        rootView.btCover.setOnClickListener{
            context.startActivity(Intent(context,CoverListAty::class.java))
        }
        rootView.btSpendHis.setOnClickListener {
            context.startActivity(Intent(context,SpendHistoryAty::class.java))
        }
        rootView.btOrder.setOnClickListener {
            context.startActivity(Intent(context,OrderAty::class.java))

        }
        rootView.btMessage.setOnClickListener {
            context.startActivity(Intent(context,SystemMessageAty::class.java))

        }
        rootView.btLocation.setOnClickListener {
            context.startActivity(Intent(context,LocationAty::class.java))
        }
        rootView.btAsk.setOnClickListener {
            OkGo.post<IsTeacherBean>(Urls.BaseUrl+Urls.ISTEACH)
                    .execute(object :JsonCallBack<IsTeacherBean>(activity){
                        override fun onSuccess(response: Response<IsTeacherBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                when(bean?.data.status){
                                  1->{
                                      MyUtils.toast("审核中")
                                  }
                                    2->{
                                        MyUtils.toast("已通过")
                                    }
                                    3->{
                                        MyUtils.toast("审核被拒绝")

                                    }
                                }
                            }else if (bean?.code==0){
                                context.startActivity(Intent(context,AskAty::class.java))

                            }
                        }
                    })
        }
        rootView.btSetting.setOnClickListener {
            context.startActivity(Intent(context,SettingsAty::class.java))
        }

        rootView.btCart.setOnClickListener {
            context.startActivity(Intent(context,CartAty::class.java))

        }



    }

    override fun onResume() {
        super.onResume()

    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    public fun handRefresh(refreshEvent: RefreshEvent){
        rootView.post {
            rootView.redB.visibility=View.VISIBLE
        }
    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    public fun handRefresh(refreshEvent: PreEvent){
        rootView.post {
            rootView.redB.visibility=View.GONE
        }
    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    public fun refreshFgm(refresh:RefreshFgmEvent){
        getUser()
    }

}